import 'package:flutter/material.dart';
import 'package:hello_flutter/base/index.dart';
import 'package:hello_flutter/widgets/keep_alive_wrapper.dart';

class TabBarViewWidget extends BasePage {
  const TabBarViewWidget({super.key});

  @override
  String getPageTitle() => 'TabBarView';

  @override
  Widget buildBody(BuildContext context) {
    return const _TabBarViewRoute();
  }
}

class _TabBarViewRoute extends StatefulWidget {
  const _TabBarViewRoute({super.key});

  @override
  State<_TabBarViewRoute> createState() => _TabBarViewRouteState();
}

class _TabBarViewRouteState extends State<_TabBarViewRoute>
    with SingleTickerProviderStateMixin {
  late TabController _tabController;
  List<String> tabs = ['新闻', '历史', '图片'];

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: tabs.length, vsync: this);
  }

  /*@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('TabBarView'),
        bottom: TabBar(
            controller: _tabController,
            tabs: tabs.map((e) => Text(e)).toList()),
      ),
      body: TabBarView(
          controller: _tabController,
          children: tabs.map((e) {
            return Container(
              alignment: Alignment.center,
              child: Text(e, textScaler: const TextScaler.linear(5)),
            );
          }).toList()),
    );
  }*/

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Container(
          height: 40,
          color: Theme.of(context).colorScheme.onPrimary,
          child: TabBar(
            controller: _tabController,
            tabs: tabs.map((e) => Text(e)).toList(),
          ),
        ),
        Expanded(
          child: TabBarView(
              controller: _tabController,
              physics: const BouncingScrollPhysics(),
              children: tabs.map((e) {
                return KeepAliveWrapper(
                    child: Container(
                  alignment: Alignment.center,
                  child: Text(e, textScaler: const TextScaler.linear(5)),
                ));
              }).toList()),
        ),
      ],
    );
  }

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }
}
